<?php
namespace app\home\controller;

use app\home\common\controller\Common;

use think\Request;
use	think\Db;
use think\facade\Session;
use think\facade\Cookie;

class Index extends Common
{
	public function index()
    {
        $trees = Db::table("rela")->field("rela.*, user.username")->leftjoin("user", "rela.max_id = user.id")->where("max_id", Session::get("userInfo.id"))->select();
        $list = array();
        foreach ($trees as $index1 => $tree) {
            $list[$index1]["username"] = $tree["username"];
            $paths = explode(",", $tree["path"]);
            array_shift($paths);
            foreach ($paths as $index2 => $path) {
                $treeUser = Db::table("user")->where("id", $path)->find();
                $list[$index1]["child"][$index2]["username"] = $treeUser["username"];
                $money = Db::table("reward")->where(["user_id" => $path, "superior_id" => Session::get("userInfo.id")])->value("money");
                $money = $money ? $money : 0;
                $list[$index1]["child"][$index2]["money"] = $money;
            }
        }
        $revenues = Db::table("revenue")->where("user_id", Session::get("userInfo.id"))->select();
        foreach ($revenues as $index => $revenue) {
            $rewards = Db::table("reward")->field("reward.*, user.username")->leftjoin("user", "reward.user_id = user.id")->where(["reward.superior_id" => Session::get("userInfo.id"), "reward.months" => $revenue["months"]])->select();
            $revenues[$index]["child"] = $rewards;
        }
        $this->assign(["trees" => $list, "revenues" => $revenues]);
        return view();
    }
    public function registered(Request $request)
    {
        if($request->isPost()) {
            $post = $request->post();
            if(!Db::table("user")->where("username", $post["username"])->find()) {
                $post["password"] = md5($post["password"]);
                $userId = Db::table("user")->insertGetId($post);
                if($post["superior_id"]) {
                    if($data = Db::table("rela")->where("min_id", $post["superior_id"])->where("level", "<", 7)->select()) {
                        $rela_id_list = "";
                        foreach ($data as $row) {
                            $rela_id_list = $rela_id_list . "," . $row["id"];
                            $relaData = ["path" => $row["path"] . "," . $userId, "min_id" => $userId, "level" => $row["level"] + 1];
                            Db::table("rela")->where("id", $row["id"])->update($relaData);
                        }
                        $relaData = ["max_id" => $post["superior_id"], "path" => $post["superior_id"] . "," . $userId, "min_id" => $userId];
                        $relaId = Db::table("rela")->insertGetId($relaData);
                            $rela_id_list = $rela_id_list . "," . $relaId;
                            $rela_id_list = ltrim($rela_id_list, ",");
                            $userData = ["rela_id_list" => $rela_id_list];
                            Db::table("user")->where("id", $userId)->update($userData);
                            $data = Db::table("user")->where('id', $post["superior_id"])->find();
                            $rela_id_list = $data["rela_id_list"] . "," . $relaId;
                            $userData = ["rela_id_list" => $rela_id_list];
                            Db::table("user")->where("id", $data["id"])->update($userData);
                            $this->success("注册成功！", "index");
                    }else {
                        $relaData = ["max_id" => $post["superior_id"], "path" => $post["superior_id"] . "," . $userId, "min_id" => $userId];
                        $relaId = Db::table("rela")->insertGetId($relaData);
                        $str = $post["superior_id"] . "," . $userId;
                        $data = Db::table("user")->where("id", "in", $str)->select();
                        foreach ($data as $row) {
                            $rela_id_list = $row["rela_id_list"] . "," . $relaId;
                            $rela_id_list = ltrim($rela_id_list, ",");
                            $userData = ["rela_id_list" => $rela_id_list];
                            Db::table("user")->where("id", $row["id"])->update($userData);
                        }
                        $this->success("注册成功！", "index");
                    }
                }else {
                    $this->success("注册成功！", "index");
                }
            }else {
                $this->error("用户名已存在！");
            }
        }else {
            $data = Db::table("user")->order("id", "desc")->select();
            $this->assign("data", $data);
            return view();
        }
    }
    public function login(Request $request)
    {
        if($request->isPost()) {
            $post = $request->post();
            $post["password"] = md5($post["password"]);
            if($user = Db::table("user")->where("username", $post["username"])->find()) {
                if($post["password"] == $user["password"]) {
                    if(isset($post["remember"])) {
                        Cookie::set("username", $user["username"], 604800);
                        Cookie::set("password", $user["password"], 604800);
                    }
                    $user["superiorname"] = $user["superior_id"] != 0 ? $user["superiorname"] = Db::table("user")->where("id", $user["superior_id"])->value("username") : "BOSS";
                    unset($user["password"]);
                    Session::set("userInfo", $user);
                    $this->success("登录成功", "index");
                }else {
                    $this->error("输入的密码有误！");
                }
            }else {
                $this->error("该用户名不存在");
            }
        }else {
            return view();
        }
    }
    public function logout()
    {
        Session::delete("userInfo");
        $this->success("退出成功", "login");
    }
}
